# -*- coding:utf-8 -*-
"""
这里通过操作chromedriver来上传本地图片到网上图床，并获取到网络url

这里提供的图床有：
    薄荷图床：'https://riyugo.com/'
    ImgUrl: 'https://imgurl.org/'

提示：
    图片安全性与


"""
import time
from selenium import webdriver
import win32gui
import win32con


def uploadToRiyugo(src_path,chromedriverExe=''):
    """
    上传图片到薄荷图床
    :param src_path: 需要上传图片的原始路径
    :param chromedriverExe:  chromedriver.exe驱动路径，如果设置了
    :return: 返回上传后的网络图片url
    """

    browser = webdriver.Chrome(executable_path=chromedriverExe if chromedriverExe else "chromedriver")

    url = 'https://riyugo.com/'
    browser.get(url)
    browser.refresh()
    browser.set_window_size(500,800)

    btn = browser.find_element_by_class_name('uploader-btn-browse') # 不需要前面修饰，只需要最后的名字
    btn.click()
    time.sleep(0.5)

    dialog = win32gui.FindWindow('#32770', '打开')  # 对话框
    ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, 'ComboBoxEx32', None)
    ComboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, 'ComboBox', None)
    Edit = win32gui.FindWindowEx(ComboBox, 0, 'Edit', None)
    button = win32gui.FindWindowEx(dialog, 0, 'Button', None)
    win32gui.SendMessage(Edit, win32con.WM_SETTEXT, None, src_path)
    win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)

    time.sleep(0.5)
    sub_btn = browser.find_element_by_class_name('uploader-btn-start')
    sub_btn.click()
    time.sleep(0.5)
    ele_links = browser.find_element_by_id('links')
    browser.quit()
    return ele_links.text


def uploadToImgurl(src_path,chromedriverExe=''):
    """
    上传图片到imgurl图床
    :param src_path: 需要上传图片的原始路径
    :param chromedriverExe:  chromedriver.exe驱动路径，如果设置了
    :return: 返回上传后的网络图片url
    """
    browser = webdriver.Chrome(executable_path=chromedriverExe if chromedriverExe else "chromedriver")
    url = 'https://imgurl.org/'
    browser.get(url)
    browser.refresh()
    browser.set_window_size(500,800)
    time.sleep(0.5)
    btn = browser.find_element_by_id('upimg')
    btn.click()
    time.sleep(0.5)

    dialog = win32gui.FindWindow('#32770', '打开')  # 对话框
    ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, 'ComboBoxEx32', None)
    ComboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, 'ComboBox', None)
    Edit = win32gui.FindWindowEx(ComboBox, 0, 'Edit', None)
    button = win32gui.FindWindowEx(dialog, 0, 'Button', None)

    win32gui.SendMessage(Edit, win32con.WM_SETTEXT, None, src_path)
    win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)

    time.sleep(0.5)
    url_text = browser.find_element_by_id('url')
    url = url_text.get_attribute('value')
    browser.quit()
    return url



def uploadToImgtu(src_path,chromedriverExe=''):
    """
    上传图片到imgtu图床
    :param src_path: 需要上传图片的原始路径
    :param chromedriverExe:  chromedriver.exe驱动路径，如果设置了
    :return: 返回上传后的网络图片url
    """

    browser = webdriver.Chrome(executable_path=chromedriverExe if chromedriverExe else "chromedriver")

    url = 'https://imgtu.com/'
    browser.get(url)
    browser.refresh()
    browser.set_window_size(500,800)
    # time.sleep(3)
    btn = browser.find_element_by_class_name('orange') # 不需要前面修饰，只需要最后的名字
    btn.click()
    time.sleep(1)

    dialog = win32gui.FindWindow('#32770', '打开')
    ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, 'ComboBoxEx32', None)
    ComboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, 'ComboBox', None)
    Edit = win32gui.FindWindowEx(ComboBox, 0, 'Edit', None)
    button = win32gui.FindWindowEx(dialog, 0, 'Button', None)

    win32gui.SendMessage(Edit, win32con.WM_SETTEXT, None, src_path)
    win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)
    time.sleep(1)
    upload_btn = browser.find_element_by_class_name('green')
    upload_btn.click()
    time.sleep(3)
    url_link = browser.find_element_by_id('embed-code-1')
    url = url_link.get_attribute('value')

    browser.quit()
    return url







